package com.sunday.framework.common.mvc;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.sunday.common.core.utils.DateTimeUtils;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDateTime;

/**
 * @Description: 全局配置
 * <p>
 * https://www.cnblogs.com/zhaord/p/14857359.html
 * https://www.cnblogs.com/huzi007/p/11726282.html
 * @Author: zsy
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    /**
     * 1. 通过输出结果和查看网络的内容，发现实际上id返回的结果是1234567890102349000，最后几位都变成了00, 这是因为，javascript的Number类型最大长度是17位，而后端返回的Long类型有19位，导致js的Number不能解析。
     * 序列化json时,将所有的long变成string
     * 因为js中得数字类型不能包含所有的java long值
     * 2. 与 Ant Design Pro对接的时候，ProFormDateTimePicker等时间类型组件要求的时间类型传输问题
     */
    @Bean
    @Primary
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        Jackson2ObjectMapperBuilderCustomizer customizer = jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
                .serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeUtils.DATETIME_FORMATTER_SPLIT))
                .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeUtils.DATETIME_FORMATTER_SPLIT))
                .serializerByType(Long.class, ToStringSerializer.instance)
                .serializerByType(Long.TYPE, ToStringSerializer.instance);
        return customizer;
    }

}
